import utils from '@/mixins/utils.js';
export default {
	mixins: [utils],
	data() {
		return {
			parkConfigInfoList: [],
			pageOpt: {
				pageNumber: 1,
				pageSize: 9,
				sortType: 'ASC'
			},
			searchVal: '',
			curPageData: null
		};
	},
	created() {
		const self = this;
		window.addEventListener('storage', function(ev) {
			if (ev.key == 'parkinglotSettingMsg') {
				if (!ev.newValue) return;
				var message = JSON.parse(ev.newValue);
				if (message === 'updateList') {
					self.getOperatorParkConfigInfoList();
				}
			}
		});
	},

	mounted() {
		this.setPageSize();
		this.getOperatorParkConfigInfoList();
	},

	methods: {
		setPageSize() {
			const fullWidth = $('#list-box').width();
			const itemWidth = 384;
			console.log(Math.floor(fullWidth / itemWidth));
			this.pageOpt.pageSize = Math.floor(fullWidth / itemWidth) * 3;
		},
		handleCurrentChange(val) {
			this.pageOpt.pageNumber = val;
			this.getOperatorParkConfigInfoList();
		},
		search() {
			this.pageOpt.pageNumber = 1;
			this.getOperatorParkConfigInfoList();
		},
		getOperatorParkConfigInfoList() {
			API.fetchPost(
				`/operatorPark/getOperatorParkConfigListView?operatorId=${sessionStorage.getItem(
					'_operatorID'
				)}&parkName=${this.searchVal}`,
				this.pageOpt,
				{
					headers: {
						'Content-Type': 'application/json'
					}
				}
			).then(
				res => {
					var arr = new Array();
					if (res.data && res.data.list && res.data.list.length) {
						res.data.list.forEach(item => {
							item.isShow = false;
						});
						this.parkConfigInfoList = res.data.list;
						this.curPageData = {
							totalRow: res.data.totalRow,
							pageNumber: res.data.pageNumber
						};
					} else {
						this.parkConfigInfoList = [];
						this.curPageData = null;
					}
				},
				err => {}
			);
		},
		getVemsUrl(parkCode) {
			API.fetchGet('/parkingBaseData/getVemsUrl.do', {
				parkCode: parkCode
			}).then(
				res => {
					if (res.status === 1 && res.resultCode === 0) {
						window.open(res.data);
					}
				},
				err => {}
			);
		},
		confirmFn(msg) {
			return new Promise(resolve => {
				this.$confirm(msg, '警告!', {
					confirmButtonText: '确认下架',
					cancelButtonText: '取消',
					customClass: 'mgr-msg-box',
					confirmButtonClass: 'el-button el-button--danger'
				})
					.then(() => {
						resolve(true);
					})
					.catch(() => {
						resolve(false);
					});
			});
		},
		async stopService(parkId) {
			const confirm_result = await this.confirmFn('车场下架后不能用手机端查费缴费，确认要下架吗?');
			if (confirm_result) {
				API.fetchGet('/operatorPark/underPark', {
					parkId: parkId
				}).then(
					res => {
						if (res) {
							if (res.status === 1) {
								this.showToast('车场下架成功', 1, 1500);
								this.getOperatorParkConfigInfoList();
							} else {
								this.showToast(res.message, 1, 1500);
							}
						}
					},
					err => {}
				);
			}
		},
		linkToNewInterComeSettingPage() {
			const routeData = this.$router.resolve({ name: 'editInterCome' });
			window.open(routeData.href, '_blank');
		}
	}
};
